package cc.xuanhaojituan.mybatis.service;

import cc.xuanhaojituan.mybatis.domain.User;
import cc.xuanhaojituan.mybatis.mapper.UserMapper;
import cc.xuanhaojituan.mybatis.util.GenericMapper;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class UserService extends GenericService<User> {

	@Autowired
	private UserMapper userMapper;

	@Transactional(readOnly = true)
	public PageInfo<User> findAll(String userName, Date birthday, Integer page, Integer pageSize) {
		PageHelper.startPage(page, pageSize);

		Example example = new Example(User.class);
		if (!StringUtils.isEmpty(userName)) {
			example.createCriteria().andLike("userName", "%" + userName + "%");
		}
		if (birthday != null) {
			example.createCriteria().andEqualTo("birthday", birthday);
		}
		List<User> users = userMapper.selectByExample(example);
		return new PageInfo<>(users);

	}

	@Override
	GenericMapper<User> getMapper() {
		return userMapper;
	}
}
